package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel>implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //1.参数检测
      if(dto==null){
          return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
      }
        //分页参数检查
       dto.checkParam();
        //安装名称模糊分页查询
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            //根据名称模糊查询
            lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
        }
        IPage<AdChannel> result = page(page, lambdaQueryWrapper);
        //结果封装
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        //检查参数
         if(channel==null){
             return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
         }
        //补全实体类属性
        channel.setIsDefault(false);
         channel.setCreatedTime(new Date());
        //保存实体类
        save(channel);
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult update(AdChannel channel) {
        //检查参数
        if(channel==null || channel.getId()==null){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //执行修改
        updateById(channel);
        //返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //参数是否正确
      if(id==null){
        return   ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
      }
        //先查询 如果不在 执行删除
        AdChannel adChannel = getById(id);
      if(adChannel==null){
        return   ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在");
      }
      if(adChannel.getStatus()){
          return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效不能删除");
      }
        removeById(id);
        //返回结果
        return ResponseResult.okResult();
    }
}
